package com.gxwz.document.business.util;

import java.lang.reflect.Method;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxwz.document.business.entity.User;
import com.gxwz.document.business.entity.LoginRecord;

import com.gxwz.document.business.service.IloginRecordService;
import com.opensymphony.xwork2.ActionContext;

@Aspect
public class LogAspect {
	 public Integer id=null;   
	    @Autowired  
	    IloginRecordService loginRecordService;  
	    @Pointcut("execution(* com.gxwz.document.business.action.*.login(..))")
	    public void loginCell(){  
	    }   
	    @AfterReturning(value = "loginCell()", returning = "object")  
	    public void loginLog(JoinPoint joinPoint, Object object) throws Throwable {
	    	String clazz =joinPoint.getTarget().getClass().getName(); 
	    	HttpServletRequest request= (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			System.out.println("IP地址：" + request.getRemoteAddr());
			Map<String, Object> session = (Map<String, Object>) ActionContext
					.getContext().getSession();	
			// 读取session中的用户
			User user = (User) session.get("user");
			System.out.println("用户名:" + user.getUserName());
	        // 获取方法名  
	        String methodName = joinPoint.getSignature().getName();  
	        // 获取操作内容  
	        String opContent = optionContent(joinPoint.getArgs(), methodName);  
	        System.out.println(111111); 
	        LoginRecord log = new LoginRecord();
	        log.setUserId(user.getUserId());
	        log.setLoginIP(request.getRemoteAddr());			
			log.setUserName(user.getUserName());
	        log.setLoginTime(new Date());
	        log.setOperateContent("登录系统");
	        loginRecordService.save(log);  
	    }  
	      
	    /** 
	     * 使用Java反射来获取被拦截方法(insert、update)的参数值， 将参数值拼接为操作内容 
	     *  
	     * @param args 
	     * @param mName 
	     * @return 
	     */  
	    public String optionContent(Object[] args, String mName) {  
	        if (args == null) {  
	            return null;  
	        }  
	        StringBuffer rs = new StringBuffer();  
	        rs.append(mName);  
	        String className = null;  
	        int index = 1;  
	        // 遍历参数对象  
	        for (Object info : args) {  
	            // 获取对象类型  
	            className = info.getClass().getName();  
	            className = className.substring(className.lastIndexOf(".") + 1);  
	            rs.append("[参数" + index + "，类型:" + className + "，值:");  
	            // 获取对象的所有方法  
	            Method[] methods = info.getClass().getDeclaredMethods();  
	            // 遍历方法，判断get方法  
	            for (Method method : methods) {  
	                String methodName = method.getName();  
	                // 判断是不是get方法  
	                if (methodName.indexOf("get") == -1) {// 不是get方法  
	                    continue;// 不处理  
	                }  
	                Object rsValue = null;  
	                try {  
	                    // 调用get方法，获取返回值  
	                    rsValue = method.invoke(info);  
	                } catch (Exception e) {  
	                    continue;  
	                }  
	                // 将值加入内容中  
	                rs.append("(" + methodName + ":" + rsValue + ")");  
	            }  
	            rs.append("]");  
	            index++;  
	        }  
	        return rs.toString();  
	    }  
	  
	  
	   
	

}
